1 LIBRARIES IN PROCESSING
3 Some basic instructions on how libraries work with Processing.
4 (I'm also adding to this as people have trouble with it..
5 Pretty soon it's gonna be an outrageous mess!)
7 Libraries are a new feature that are present only in revisions 70
8 and higher. Before revision 70, users could place any sort of code
9 inside the 'code' folder of their sketch, but this meant too many
10 copies of each library. The code folder is still an option, but the
11 use of the new "library" system is encouraged as a simple packaging
12 mechanism for your projects.
14 Libraries live inside the "libraries" folder of the Processing
15 distribution, however users are encouraged to install "contributed"
16 libraries in their sketchbook folder, which will prevent them
17 from having to move their libraries with new Processing releases.
19 A Processing library can be any sort of Java code that's been
20 given a package name and packed into a jar file. It can also register
21 itself with the parent applet to get notification of when events
22 happen in the sketch, for instance whenever draw() is called or a
25 Most libraries may not need that much functionality, but they
26 may want to implement the dispose() call, which is called as the
27 applet is closed. Many libraries, especially those with native code,
28 need this to properly shut down.
30 It is not possible to build libraries from within Processing itself.
31 In fact, creating a library with Processing will cause problems
32 because when exported as an applet, the jar file will contain the
33 current version of the processing.core classes, which will cause
34 major conflicts when trying to use any other code.
36 The PDE is built with the sole purpose of creating short sketches that
37 are part of PApplet that have a few files at most. We don't plan to
38 extend the PDE to also support developing libraries (or "tools," once
39 those are enabled) because then it simply becomes like any other IDE
40 that is quickly too general for our target audience. Users who are
41 advanced enough in their programming skills to build libraries will
42 almost always be skilled enough to use another IDE like Eclipse
43 (if they aren't already) to build their library.
46 //////////////////////////////////////////////////////////////
49 Notes on Library distribution
51 There are two categories of libraries. The "core" libraries
52 (Video, OpenGL, Serial, Net) are part of the Processing distribution,
53 and "contributed libraries" are developed, owned, and maintained
54 by members of the Processing community.
56 It's very possible that contributed libraries might make their way
57 into the regular distribution if it makes sense for all involved.
58 After the 1.0 release, we'll re-evaluate if some libraries should be
59 made a part of the standard distribution. For now, we don't have the
60 people resources to support this, because it would require us to
61 debug the libraries with each release.
63 We try to place a strong focus on the importance of clear
64 documentation for the Processing project, so please attempt similar
65 effort into communicating your library's features to potential users
66 by hosting a descriptive web site.
68 If you'd like to have your library posted on the Processing website
69 (http://processing.org/reference/libraries) please email
70 reas at processing.org and we'll make a decision about its inclusion.
71 We strongly encourage (and may someday require as a stipulation for
72 placement on the site) that the source to your library be included.
73 We're giving away all our stuff, and we want others to do so as well
74 because it's good for the community.
76 The contributed libraries are one of the most important aspects of
77 the Processing project and have an enormous impact on how people
78 understand Processing. Libraries have been designed into the larger
79 Processing plan to enable simple extensions of the core API in new,
80 innovative, and unexpected directions. The libraries are the future
81 of the project as we plan for processing.core.* to remain very
85 //////////////////////////////////////////////////////////////
91 package libraryexample;
92 import processing.core.*;
94 public class BoringLibrary {
97 public BoringLibrary(PApplet parent) {
99 parent.registerDispose(this);
102 public void dispose() {
103 // anything in here will be called automatically when
104 // the parent applet shuts down. for instance, this might
105 // shut down a thread used by this library.
106 // note that this currently has issues, see bug #183
107 // http://dev.processing.org/bugs/show_bug.cgi?id=183
112 Usually you'll need to pass "this" to a library's constructor so that
113 the library can access functions from PApplet, i.e graphics methods
114 like line() and stroke() or loadImage(). See later in the document for
115 information about reading and writing files.
117 If you'd like to use constants such as "RGB", use the following:
118 public class BoringLibrary implements PConstants {
119 which will allow all the constants to be used by that class.
122 //////////////////////////////////////////////////////////////
128 method that's called just after beginFrame(), meaning that it
132 method that's called at the end of draw(), but before endFrame().
134 public void mouseEvent(MouseEvent e)
135 called when a mouse event occurs in the parent applet
137 public void keyEvent(KeyEvent e)
138 called when a key event occurs in the parent applet
141 method called after draw has completed and the frame is done.
144 public void size(int width, int height)
145 this will be called the first time an applet sets its size,
146 but also any time that it's called while the PApplet is running.
147 no drawing should occur inside of this method, because it may
148 not be the case that the new renderer is yet valid and ready.
149 use this only to flag the new size and prepare for the next frame.
152 can be called by users, for instance movie.stop() will shut down
153 a movie that's being played, or camera.stop() stops capturing
154 video. server.stop() will shut down the server and shut it down
155 completely, which is identical to its "dispose" function.
157 public void dispose()
158 this should only be called by PApplet. dispose() is what gets
159 called when the host applet is stopped, so this should shut down
160 any threads, disconnect from the net, unload memory, etc.
161 currently, this method is not being called consistently:
162 http://dev.processing.org/bugs/show_bug.cgi?id=77
164 To register any of these methods with the parent, call
165 parent.registerPre(this) or whatever the name of the function
166 is that you'd like to use.
168 Note that making things "public" is extremely important. When running
169 inside Processing, anything left blank has public added by the
170 preprocessor, meaning "void draw()" becomes "public void draw()".
172 You can only draw inside of pre(), draw(), mouseEvent(), or keyEvent()
173 otherwise you may run into trouble. pre() and draw() happen while
174 legitimate drawing is taking place, and the mouse/key events happen
175 just before draw() events are called, they're queued up by the host
176 applet until it's safe to draw.
178 For this reason, you should use registerMouseEvent() and mouseEvent()
179 (and same for the keys) to handle your events, rather than your class
180 implementing MouseListener. For instance, to figure out what the mouse
181 event is throwing back at you, this would be an example handler:
183 public void mouseEvent(MouseEvent event) {
184 int x = event.getX();
185 int y = event.getY();
187 switch (event.getID()) {
188 case MouseEvent.MOUSE_PRESSED:
189 // do something for the mouse being pressed
191 case MouseEvent.MOUSE_RELEASED:
192 // do something for mouse released
194 case MouseEvent.MOUSE_CLICKED:
195 // do something for mouse clicked
197 case MouseEvent.MOUSE_DRAGGED:
198 // do something for mouse dragged
200 case MouseEvent.MOUSE_MOVED:
206 More on mouse handling can be found in Sun's Java documentation:
207 http://java.sun.com/j2se/1.4.2/docs/api/java/awt/event/MouseEvent.html
208 which also covers things like modifiers (shift-click) and whatnot.
209 Also check out the code for PApplet to see how ctrl-click is handled
210 on Mac OS X so that it properly registers as a right-click.
213 //////////////////////////////////////////////////////////////
216 Structure of a Library Folder
218 The Sonia library by Amit Pitaru is a good example here. To make a
219 library called sonia, you create a folder called "sonia" and within
220 that, a subfolder named "library". The sonia folder should be placed
221 inside the Processing "libraries" folder, or a user can place it
222 inside their sketchbook folder.
224 Inside "library", you'll find "sonia.jar". Anything that is found
225 inside library will be exported with your sketch.
227 If different sets of files should be exported with applets versus
228 applications, a file called "export.txt" should be included. For
229 sonia, this looks like:
231 # only export the jar file for applets..
232 # everything else is installed as a separate browser plugin
234 # application needs everything
235 application=sonia.jar,JSynClasses.jar,JSynV142.dll,libJSynV142.jnilib
237 This will include sonia.jar for applets, because in a web browser, the
238 DLL files must be installed separately along with JSynClasses.jar.
239 The # sign in front of a line means that the line is a comment,
240 and it'll be ignored by the PDE.
242 As of revision 0097, you can also specify what to export for other
243 platforms as well (at least Mac OS X, Windows, Linux). For the example
244 above, the application line could instead be changed to:
246 application.macosx=sonia.jar,JSynClasses.jar,libJSynV142.jnilib
247 application.windows=sonia.jar,JSynClasses.jar,JSynV142.dll
249 Platform-specific exports will be checked first, and if they don't
250 exist, the "application" will be used. If neither exist (or export.txt
251 doesn't exist), the entire contents of the library folder will be
255 //////////////////////////////////////////////////////////////
258 Using Other Java Code As A Library
260 So long as the code is inside a package, it can be set up for use as
261 a library. For instance, if you want to make a library called 'poopy'
262 set up a folder as follows:
268 Then, the folder should be placed in the Processing 'libraries' folder
269 or inside the user's sketch folder to be recognized by Processing and
270 its "Import Library" menu. As of now, you may need to restart
271 Processing in order to get the library to show up.
273 While this process may sound a little complicated, the intent is to
274 make it easier for users than a typical Java IDE. A little added
275 complexity for the developers of library code (who will generally be
276 more advanced users) is traded for great simplicity by the users,
277 since Processing is intended to target beginning programmers.
280 //////////////////////////////////////////////////////////////
283 Import Statements and How They Work
285 If your library is sonia.jar, found at sonia/library/sonia.jar, all
286 the packages found in sonia.jar will be added as imports into the
287 user's sketch when they selected "Import Library".
289 In the case of Sonia, an additional .jar file can be found in the
290 sonia/library/ folder, jsyn.jar. The contents of jsyn.jar will not be
291 added to the import statements. This is to avoid every library having
292 a ridiculously large number of import statements. For instance, if you
293 want to use the "video" library, you don't want all 15-20 packages for
294 the QuickTime libraries listed there to confuse your users.
296 Bottom line, if you want packages from the other .jar to be loaded by
297 Processing, then you need to put those .class files into the main .jar
298 file for the library (sonia/library/sonia.jar in this case).
301 //////////////////////////////////////////////////////////////
304 Import Statements and the Code Folder
306 The code folder works differently, and every package inside every .jar
307 found in the code folder is simply added to the import statements by
308 the preprocessor. The user never sees this, it just happens magically.
311 //////////////////////////////////////////////////////////////
314 Creating .jar Files For Your Library
316 Since your code is inside a package, you need to make sure that it's
317 inside subfolders in the .jar file. It should be noted that jar files
318 are simply .zip files (they can be opened with WinZip or Stuffit) with
321 In the past, you may have used:
323 to compile your files. Once they're inside a packages, you must use:
325 which will create folders and subfolders for the packages. For
326 instance, for all the stuff in processing.core.* it would create:
334 then you can jar that stuff up using:
335 jar -cf core.jar processing
336 or with the command line info-zip utility:
337 zip -r core.jar processing
340 //////////////////////////////////////////////////////////////
343 The "Import Library" Menu Item
345 All this does is add the "import yourlibrary.*;" statement to the top
346 of your sketch. If you've handwritten the import statements, then
347 there's no need to use "Import Library".
350 //////////////////////////////////////////////////////////////
353 Getting a UnsupportedClassVersionError? (Java 1.5 and later)
355 When I compiled blah.jar (using the successful method mentioned
356 earlier) under Java 1.5, I get the following error from Processing:
358 java.lang.UnsupportedClassVersionError: blah/SomeClass
359 (Unsupported major.minor version 49.0)
361 This is because more recent versions of Java like to use their own
362 class file format that's not backwards compatible.
364 When compiling a library, use somtehing like:
365 javac -source 1.4 -target 1.4 -d . \
366 -classpath /path/to/core.jar path/to/java/source/*.java
369 //////////////////////////////////////////////////////////////
372 Adding Your Own Library Events
374 So that your library can notify the host applet that something
375 interesting has happened, this is how you implement an event
376 method in the style of serialEvent, serverEvent, etc.
379 public class FancyLibrary {
380 Method fancyEventMethod;
382 public YourLibrary(PApplet parent) {
383 // your library init code here...
385 // check to see if the host applet implements
386 // public void fancyEvent(FancyLibrary f)
389 parent.getClass().getMethod("fancyEvent",
390 new Class[] { FancyLibrary.class });
391 } catch (Exception e) {
392 // no such method, or an error.. which is fine, just ignore
396 // then later, to fire that event
397 public void makeEvent() {
398 if (fancyEventMethod != null) {
400 fancyEventMethod.invoke(parent, new Object[] { this });
401 } catch (Exception e) {
402 System.err.println("Disabling fancyEvent() for " + name +
403 " because of an error.");
405 fancyEventMethod = null;
411 //////////////////////////////////////////////////////////////
414 Using built-in functions from processing.core
416 Many methods in PApplet are made static for use by other libraries
417 and code that interfaces with Processing. For instance, openStream()
418 requires an applet object, but loadStrings() is a static method
419 that can be run on any InputStream. See the developer's reference
420 for more information about which methods are available.
423 //////////////////////////////////////////////////////////////
426 Accessing files from inside a library
428 To open files for use with a library, use the openStream() method.
429 This is the most compatible means for loading data, and makes use
430 of many hours of headaches that were the result of attempts to
431 create functions that loaded data across platforms (Mac, Windows,
432 and Linux) and circumstances (applet, application, and other).
434 The functions sketchPath(), savePath(), dataPath(), and createPath()
435 all facilitate reading and writing files relative to the sketch
436 folder. They should be used to ensure that file I/O works consistently
437 between your library and functions like loadImage() or loadStrings().
438 Their documentation can be seen in the online javadoc reference found
439 on dev.processing.org. The variable sketchPath is available for
440 convenience, but in nearly all cases, the sketchPath() method is
441 a better (and more compatible) route.
443 The xxxxPath() functions were finalized in revision 0096.
446 //////////////////////////////////////////////////////////////
451 Libraries, or classes inside them, should not be prefixed with "P"
452 the way that the core Processing classes are (PImage, PGraphics, etc).
453 It's tempting to prefix everything that way to identify it with
454 Processing, but we'd like to reserve that naming for "official"
455 things that are inside processing.core and other associated classes.
457 Same goes for using "Processing", "Pro", or "P5" just like "P",
458 or whether it's a prefix or a suffix.
460 Similarly, please don't using processing.* as the prefix for your
461 library packages. We'd like to keep that name space clear for
462 official things as well.
465 //////////////////////////////////////////////////////////////
468 Ben Fry, Last updated 23 October 2007